﻿// TempColorizer.cs
//
// Copyright 2008 by Burt Harris
// Licensed under the Microsoft Public License (Ms-PL)
// See: http://www.codeplex.com/yaml/license
//
using Microsoft.VisualStudio.TextManager.Interop;
using System;
using System.Collections;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Text;
using System.Drawing;
using Microsoft.VisualStudio.Package;
using System.Collections.Generic;

namespace YaTools.Yaml.LanguagePackage
{
    /// <include file='doc\Colorizer.uex' path='docs/doc[@for="Colorizer"]/*' />
    [CLSCompliant(false)]
    [System.Runtime.InteropServices.ComVisible(true)]
    public class TempColorizer : Microsoft.VisualStudio.Package.Colorizer
    {
        static TraceSource tracer = new TraceSource("TempColorizer", SourceLevels.Warning);
        List<TempColorizer> _collection;
        public IVsTextLines Buffer;

        /// <include file='doc\Colorizer.uex' path='docs/doc[@for="Colorizer.Colorizer"]/*' />
        public TempColorizer(LanguageService svc, IVsTextLines buffer, 
            IScanner scanner, List<TempColorizer> collection)
            : base(svc, buffer, scanner)
        {
            Buffer = buffer;
            _collection = collection;
            _collection.Add(this);
        }

        public override void  CloseColorizer()
        { 
             _collection.Remove( this );
             base.CloseColorizer();
        }

        public override int ColorizeLine(int line, int length, IntPtr ptr, int state, uint[] attrs)
        {
            if (attrs != null)
                tracer.TraceEvent(TraceEventType.Information, 2, "ColorizeLine {0}, length={1}, state={2}", line+1, length, state);

            var scanner = Scanner as VsYamlScanner;
            if (scanner != null) scanner.ColorizingLine = line+1;
            return base.ColorizeLine(line, length, ptr, state, attrs);
        }

        public override TokenInfo[] GetLineInfo(IVsTextLines buffer, int line, IVsTextColorState colorState)
        {
            var result = base.GetLineInfo(buffer, line, colorState);
            return result;
        }
    
        public override int GetColorInfo(string line, int length, int state)
        {
            tracer.TraceEvent(TraceEventType.Information, 5, "GetLineInfo({1},{2}): {0}", line+1, length+1, state);
            return base.GetColorInfo(line, length, state);
        }

        public override int GetStartState(out int start)
        {
            int temp;
            int value = base.GetStartState(out temp);
            start = temp;
            tracer.TraceEvent(TraceEventType.Information, 0, "GetStartState {0} = {1}", start, temp);
            return value;
        }

        public override int GetStateAtEndOfLine(int line, int length, IntPtr ptr, int state)
        {
            tracer.TraceEvent(TraceEventType.Information, 1, "GetStateAtEOL {0}, length={1}, state={2}", line, length, state);
            return base.GetStateAtEndOfLine(line, length, ptr, state);
        }

        public override void Suspend()
        {
            tracer.TraceEvent(TraceEventType.Information, 7, "Suspend");
            base.Resume();
        }
 
        public override void Resume()
        {
            tracer.TraceEvent(TraceEventType.Information, 8, "Resume");
            base.Resume();
        }
    }
}